1 using System;
2 using
System.Collections.Generic;
3 using
ExitGames.Client.Photon;
4 using
UnityEngine;
5 using
Hashtable = ExitGames.Client.Photon.Hashtable;
6
7
8 ///
<summary>
9 ///
Implements teams in a room/game with help of player properties. Access them by PhotonPlayer.GetTeam extension.
10 ///
</summary>
11 ///
<remarks>
12 ///
Teams are defined by enum Team. Change this to get more / different teams.
13 ///
There are no rules when / if you can join a team. You could add this in JoinTeam or something.
14 ///
</remarks>
15 public
class PunTeams : MonoBehaviour
16 {

17     ///
<summary>Enum defining the teams available. First team should be neutral (it's the default value any field of this enum gets).</summary>
18     
public enum Team : byte {none, red, blue};
19
20     ///
<summary>The main list of teams with their player-lists. Automatically kept up to date.</summary>
21     ///
<remarks>Note that this is static. Can be accessed by PunTeam.PlayersPerTeam. You should not modify this.</remarks>
22     
public static Dictionary<Team, List<PhotonPlayer>> PlayersPerTeam;
23     
24     ///
<summary>Defines the player custom property name to use for team affinity of "this" player.</summary>
25     
public const string TeamPlayerProp = "team";
26
27
28     
#region Events by Unity and Photon
29
30     
public void Start()
31     {
32         PlayersPerTeam =
new Dictionary<Team, List<PhotonPlayer>>();
33         Array enumVals = Enum.GetValues(
typeof (Team));
34         
foreach (var enumVal in enumVals)
35         {
36             PlayersPerTeam[(Team)enumVal] =
new List<PhotonPlayer>();
37         }
38     }

39
40
41     ///
<summary>Needed to update the team lists when joining a room.</summary>
42     ///
<remarks>Called by PUN. See enum PhotonNetworkingMessage for an explanation.</remarks>
43     
public void OnJoinedRoom()
44     {
45         
46         
this.UpdateTeams();
47     }

48
49     ///
<summary>Refreshes the team lists. It could be a non-team related property change, too.</summary>
50     ///
<remarks>Called by PUN. See enum PhotonNetworkingMessage for an explanation.</remarks>
51     
public void OnPhotonPlayerPropertiesChanged(object[] playerAndUpdatedProps)
52     {
53         
this.UpdateTeams();
54     }
55     
56     
#endregion
57     
58
59     
public void UpdateTeams()
60     {
61         Array enumVals = Enum.GetValues(
typeof(Team));
62         
foreach (var enumVal in enumVals)
63         {
64             PlayersPerTeam[(Team)enumVal].Clear();
65         }
66
67         
for (int i = 0; i < PhotonNetwork.playerList.Length; i++)
68         {
69             PhotonPlayer player = PhotonNetwork.playerList[i];
70             Team playerTeam = player.GetTeam();
71             PlayersPerTeam[playerTeam].Add(player);
72         }
73     }
74 }

75
76 ///
<summary>Extension used for PunTeams and PhotonPlayer class. Wraps access to the player's custom property.</summary>
77 static
class TeamExtensions
78 {

79     ///
<summary>Extension for PhotonPlayer class to wrap up access to the player's custom property.</summary>
80     ///
<returns>PunTeam.Team.none if no team was found (yet).</returns>
81     
public static PunTeams.Team GetTeam(this PhotonPlayer player)
82     {
83         
object teamId;
84         
if (player.customProperties.TryGetValue(PunTeams.TeamPlayerProp, out teamId))
85         {
86             
return (PunTeams.Team)teamId;
87         }
88
89         
return PunTeams.Team.none;
90     }

91
92     ///
<summary>Switch that player's team to the one you assign.</summary>
93     ///
<remarks>Internally checks if this player is in that team already or not. Only team switches are actually sent.</remarks>
94     ///
<param name="player"></param>
95     ///
<param name="team"></param>
96     
public static void SetTeam(this PhotonPlayer player, PunTeams.Team team)
97     {
98         
if (!PhotonNetwork.connectedAndReady)
99         {
100             Debug.LogWarning(
"JoinTeam was called in state: " + PhotonNetwork.connectionStateDetailed + ". Not connectedAndReady.");
101         }
102
103         PunTeams.Team currentTeam = PhotonNetwork.player.GetTeam();
104         
if (currentTeam != team)
105         {
106             PhotonNetwork.player.SetCustomProperties(
new Hashtable() {{PunTeams.TeamPlayerProp, (byte) team}});
107         }
108     }
109 }


Implements teams in a roomgame with help of player properties. Access them by PhotonPlayer.GetTeam extension.

Teams are defined by enum Team. Change this to get more different teams.

There are no rules when if you can join a team. You could add this in JoinTeam or something.

Enum defining the teams available. First team should be neutral (it's the default value any field of this enum gets).

The main list of teams with their player-lists. Automatically kept up to date.

Note that this is static. Can be accessed by PunTeam.PlayersPerTeam. You should not modify this.

Defines the player custom property name to use for team affinity of "this" player.

Needed to update the team lists when joining a room.

Called by PUN. See enum PhotonNetworkingMessage for an explanation.

Refreshes the team lists. It could be a non-team related property change, too.

Called by PUN. See enum PhotonNetworkingMessage for an explanation.

Extension used for PunTeams and PhotonPlayer class. Wraps access to the player's custom property.

Extension for PhotonPlayer class to wrap up access to the player's custom property.

PunTeam.Team.none if no team was found (yet).

Switch that player's team to the one you assign.

Internally checks if this player is in that team already or not. Only team switches are actually sent.




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.453 lượt xem

Gõ tìm kiếm nhanh...